package com.gm.wj.dao;

import com.gm.wj.entity.SysOrder;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface SysOrderDAO extends JpaRepository<SysOrder,Integer> {

    SysOrder findById(int id);

    SysOrder findByOrderId(String id);

    @Query(value = "select * from sys_order where order_id = ?1 ",nativeQuery = true)
    SysOrder findSysOrderByOrderId(String out_trade_no);

    // 多条件模糊查询
    @Query(value = "select * from sys_order  where  (?1 is null or order_status like ?1) and (?2 is null or product_name like ?2) " +
            "and (?3 is null or order_id like ?3) and (?4 is null or username like ?4) ",nativeQuery = true)
    List<SysOrder> find(String queryKey1, String queryKey2, String queryKey3,String username);

    // 修改删除操作要加这个注解
    @Modifying
    @Query(value = "UPDATE sys_order  SET order_status=?1 WHERE id=?2 ",nativeQuery = true)
    void editStatusByOrderId(int i, int id);

    // 根据用户名、课程id查询。这个人是否报了名
    @Query(value = "select count(1) from sys_order where product_id = ?1 and username = ?2 and order_status = 1  ",nativeQuery = true)
    int isJoinByOrder(String orderId, String username);

    // 根据用户名、课程id查询。这个人是否报了名
    @Query(value = "select count(1) from sys_order where product_id = ?1 and username = ?2 and order_status = 0 ",nativeQuery = true)
    int isPayByOrder(String orderId, String username);

    @Query(value = "SELECT COUNT(*) FROM sys_order WHERE product_name LIKE '%拼团%'  and order_status = 1 ",nativeQuery = true)
    int findAllByPtStatus();

    @Query(value = "SELECT COUNT(*) FROM sys_order WHERE product_name Not  LIKE '%拼团%' and order_status = 1 ",nativeQuery = true)
    int findAllBydmStatus();

    // jpa分页查询  多条件
    Page<SysOrder> findAll(Specification<SysOrder> sysStudentSpecification, Pageable pageable);

    List<SysOrder> findFlowByUsername(String username);

    List<SysOrder> findAllByProductId(String courseId);
}
